Change read and list to return None if key/dir doesn't exist.
authorcl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Tue, 13 Sep 2005 14:45:34 +0000 (14:45 +0000)
committercl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Tue, 13 Sep 2005 14:45:34 +0000 (14:45 +0000)
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
tools/python/xen/lowlevel/xs/xs.c

index deb2365ec68419d25e940e5367213502587bef9b..78612befb061d98a89c37563e35cc18fa891f0d1 100644 (file)
@@ -74,6 +74,7 @@ static inline PyObject *pyvalue_str(char *val) {
        " path [string]: xenstore path\n"       \
        "\n"                                    \
        "Returns: [string] data read.\n"        \
+       "         None if key doesn't exist.\n" \
        "Raises RuntimeError on error.\n"       \
        "\n"
 
@@ -97,7 +98,11 @@ static PyObject *xspy_read(PyObject *self, PyObject *args, PyObject *kwds)
     xsval = xs_read(xh, path, &xsval_n);
     Py_END_ALLOW_THREADS
     if (!xsval) {
-        PyErr_SetFromErrno(PyExc_RuntimeError);
+        if (errno == ENOENT) {
+            Py_INCREF(Py_None);
+            val = Py_None;
+        } else
+            PyErr_SetFromErrno(PyExc_RuntimeError);
         goto exit;
     }
     val = PyString_FromStringAndSize(xsval, xsval_n);
@@ -160,6 +165,7 @@ static PyObject *xspy_write(PyObject *self, PyObject *args, PyObject *kwds)
        " path [string]: path to list.\n"                       \
        "\n"                                                    \
        "Returns: [string array] list of subdirectory names.\n" \
+       "         None if key doesn't exist.\n"                 \
        "Raises RuntimeError on error.\n"                       \
        "\n"
 
@@ -183,12 +189,17 @@ static PyObject *xspy_ls(PyObject *self, PyObject *args, PyObject *kwds)
     xsval = xs_directory(xh, path, &xsval_n);
     Py_END_ALLOW_THREADS
     if (!xsval) {
-        PyErr_SetFromErrno(PyExc_RuntimeError);
-        goto exit;
+        if (errno == ENOENT) {
+            Py_INCREF(Py_None);
+            val = Py_None;
+        } else
+            PyErr_SetFromErrno(PyExc_RuntimeError);
+       goto exit;
     }
     val = PyList_New(xsval_n);
     for (i = 0; i < xsval_n; i++)
         PyList_SetItem(val, i, PyString_FromString(xsval[i]));
+    free(xsval);
  exit:
     return val;
 }